from sqlalchemy import Column, Integer, String, DateTime, func
from sqlalchemy.orm import Session
from backend.app.db.database import Base
from backend.app.utils.auth import get_password_hash, verify_password as auth_verify_password

class Admin(Base):
    __tablename__ = 'admins'

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, nullable=False, index=True)
    password_hash = Column(String(255), nullable=False)

    @staticmethod
    def get_by_email(db: Session, email: str):
        """通过邮箱查找管理员"""
        return db.query(Admin).filter(Admin.username == email).first()

    def verify_password(self, password: str) -> bool:
        """验证密码"""
        return auth_verify_password(password, self.password_hash)

    @staticmethod
    def create(db: Session, username: str, password: str):
        """创建新管理员"""
        admin = Admin(
            username=username,
            password_hash=get_password_hash(password)
        )
        db.add(admin)
        db.commit()
        db.refresh(admin)
        return admin 